// 动作创建器
import {message} from 'antd'
import {color_list, color_add} from "../../services/ColorManagement";

export const FETCH_COLOR_REQUEST = 'FETCH_COLOR_REQUEST';

export function fetchColorRequest() {
    return {type: FETCH_COLOR_REQUEST};
}

export const FETCH_COLOR_SUCCESS = 'FETCH_COLOR_SUCCESS';

export function fetchColorSuccess(color_data) {
    return {type: FETCH_COLOR_SUCCESS, color_data: color_data}
}

export const ADD_COLOR_REQUEST = 'ADD_COLOR_REQUEST';

export function addColorRequest() {
    return {type: ADD_COLOR_REQUEST}
}

export const SAVE_COLOR_FAILURE = 'SAVE_COLOR_FAILURE';

function saveColorFailure(error) {
    return {type: SAVE_COLOR_FAILURE, error};
}

export const SAVE_COLOR_SUCCESS = 'SAVE_COLOR_SUCCESS';

function saveColorSuccess(params) {
    return {type: SAVE_COLOR_SUCCESS, params};
}

export function fetchColor() {
    return function (dispatch) {
        dispatch(fetchColorRequest());
        color_list({name: ''}).then((result) => {
            console.log('action123456', result.data);
            dispatch(fetchColorSuccess(result.data))
        })
    }
}

export function addColor(params) {
    return function (dispatch) {
        color_add(params)
            .then((resp) => {
                message.info('新增成功')
                dispatch(fetchColor())
            })
            .catch((err) => {
                dispatch(saveColorFailure(err))
            })
    }
}